/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.applications.flickr;

import elaborate.tag_analysis.client.TagEntry;
import elaborate.tag_analysis.client.applications.AbstractApplication;
import elaborate.tag_analysis.client.applications.ApplicationContext;
import elaborate.tag_analysis.client.applications.ResourceHandler;
import elaborate.tag_analysis.client.applications.flickr.ui.MainControlPanel;
import elaborate.tag_analysis.client.event.TagProcessingEvent;
import elaborate.tag_analysis.client.event.TagProcessingEventType;
import java.awt.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lendle
 */
public class FlickrApplication extends AbstractApplication {

    private MainControlPanel ui = null;
    private ResourceHandler resourceHandler = null;

    @Override
    public String getApplicationId() {
        return "flickr";
    }

    @Override
    public void init(ApplicationContext context) {
        super.init(context);
        this.ui = new MainControlPanel();
        this.ui.setContext(context);
        File appPropsFile=new File(super.getApplicationFolder(), "appConf.properties");
        if(appPropsFile.exists() && appPropsFile.isFile()){
            Properties appProps=new Properties();
            FileInputStream input=null;
            
            try{
                input=new FileInputStream(appPropsFile);
                appProps.load(input);
                this.ui.setAppProperties(appProps);
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                if(input!=null){
                    try {
                        input.close();
                    } catch (IOException ex) {
                        Logger.getLogger(FlickrApplication.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        else{
            this.ui.setAppProperties(null);
        }
        
        this.ui.setResourceLogs(resourceLogs);
        this.resourceHandler = new FlickrResourceHandler(this.ui, resourceLogs, this.daoFactory);
    }

    @Override
    public Component getMainView() {
        return ui;
    }

    @Override
    public void requestTags4Resource(String resourceIdentifier) {
        try {
            //throw new UnsupportedOperationException("Not supported yet.");
            List<String> tagStrs = ((FlickrResourceHandler) this.resourceHandler).getTags4URL(resourceIdentifier);
            List<TagEntry> tagEntries = new ArrayList<TagEntry>();
            for (String tag : tagStrs) {
                tagEntries.add(new TagEntry(tag));
            }


            TagProcessingEvent event = new TagProcessingEvent();
            event.setResourceIdentifier(resourceIdentifier);
            event.setTagEntries(tagEntries.toArray(new TagEntry[0]));
            context.fireTagProcessingEvent(TagProcessingEventType.TAG_EXTRACTED, event);

        } catch (Exception ex) {
            Logger.getLogger(FlickrApplication.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public ResourceHandler getResourceHandler() {
        return resourceHandler;
    }

    @Override
    public void activated() {
    }

    @Override
    public void closing() {
        super.closing();
        File appPropsFile=new File(super.getApplicationFolder(), "appConf.properties");
        FileOutputStream output=null;
        try{
            output=new FileOutputStream(appPropsFile);
            this.ui.getAppProperties().store(output, null);
        }catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(output!=null){
                try {
                    output.close();
                } catch (IOException ex) {
                    Logger.getLogger(FlickrApplication.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    
}
